const wx2my = require('../../wx2my');

// pages/pay_deposit/pay_deposit.js
const app = getApp();
Page({
  data: {
    acceptRule: true,
    num: 1,
    minusStatus: 'disable'
  },
  onLoad: function (e) {
    this.setData({
      trade_id: e.trade_id
    });
  },
  onReady: function () {
    this.get_detail();
  },
  onShow: function () {},

  get_detail() {
    let that = this;
    app.wxRequest('deposit_details', {
      'token': wx2my.getStorageSync('token'),
      'way': 1,
      'trade_id': that.data.trade_id
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          msg: res.data.data.info,
          foregift_id: res.data.data.foregift_id,
          num: res.data.data.num
        });
      }
    });
  },

  // 同意规则
  bindAcceptRule: function (e) {
    let that = this;
    that.data.acceptRule = !that.data.acceptRule;

    if (that.data.acceptRule == true) {
      this.acceptRule = true;
    } else {
      this.acceptRule = false;
    }
  },

  //确认支付
  pay() {
    if (this.data.onOff) return false;
    let that = this;

    if (that.data.acceptRule) {
      that.paying();
    } else {
      app.toast("请阅读并勾选用户协议！");
    }
  },

  paying() {
    this.setData({
      onOff: 1
    });
    let that = this;
    let money = that.data.money;
    wx.login({
      success: function (e) {
        console.log("-----", e);
        let data = {
          token: wx2my.getStorageSync('token'),
          foregift_id: that.data.foregift_id,
          code: e.code,
          type: 2
        };
        app.wxRequest('pay_deposit', data, function (res) {
          console.log(res);

          if (res.data.code == 1) {
            // let data = res.data.data.payparams.success;
            // var trade_id = res.data.data.trade_id;

               
          my.tradePay({
            tradeNO: res.data.data.payparams.trade_no, // 完整的支付参数拼接成的字符串，从 alipay.fund.auth.order.app.freeze 接口获取
            success: (res) => {
              console.log(res);
              that.setData({
                success: false
              });
              setTimeout(() => {
                wx2my.navigateBack();
              }, 2000);
            },
            fail: (res) => {
              that.setData({
                onOff: 0
              });
              console.log(err);
            }
          });
            // wx.requestPayment({
            //   'timeStamp': data.timeStamp,
            //   'nonceStr': data.nonceStr,
            //   'package': data.package,
            //   'signType': 'MD5',
            //   'paySign': data.paySign,
            //   'success': function (res) {
            //     console.log(res);
            //     that.setData({
            //       success: false
            //     });
            //     app.fm.get_prevPage().setData({
            //       foregift_id: trade_id
            //     });
            //     wx2my.navigateBack();
            //   },
            //   'fail': function (err) {
            //     that.setData({
            //       onOff: 0
            //     });
            //     console.log(err);
            //   }
            // });
          } else {
            that.setData({
              onOff: 0
            }); // setTimeout(() => {
            //   wx.navigateBack();
            // }, 2000);

            app.toast(res.data.msg);
          }
        });
      }
    });
  },

  jump_agr() {
    wx2my.navigateTo({
      url: '/pages/user_agreement/user_agreement'
    });
  },

  //事件处理函数

  /*点击减号*/
  bindMinus() {
    var num = this.data.num;

    if (num > 1) {
      num--;
    }

    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  /*点击加号*/
  bindPlus() {
    var num = this.data.num;
    num++;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  /*输入框事件*/
  bindManual(e) {
    var num = e.detail.value;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});